package pro.softzhang.algo.lc1000;

import pro.softzhang.algo.flag.knowledge.KSlidingWindow;

/**
 * 1004. 最大连续1的个数 III
 * https://leetcode.cn/problems/max-consecutive-ones-iii
 */
public class LC1004_MaxConsecutiveOnesIII {
    public static void main(String[] args) {

    }

    @KSlidingWindow
    static
    class Solution {
        public int longestOnes(int[] nums, int k) {
            int longest = 0;
            for (int left = 0, right = 0, zeros = 0; right < nums.length; right++) {
                if(nums[right] == 0) {
                    zeros++;
                }
                while (zeros > k) {
                    if(nums[left++] == 0) {
                        zeros--;
                    }
                }
                longest = Math.max(longest, right - left + 1);
            }
            return longest;
        }
    }
}
